﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using Rio.Framework.Historical;

#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Document type database row to FolderType object
    /// </summary>
    public class FolderTypeTranslator
    {
        /// <summary>
        /// Translate a bunch of FolderType DataReader to FolderType object
        /// </summary>
        /// <param name="p_lUserId"></param>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of FolderType Object</returns>
        public static List<FolderType> TranslateFolderType(long p_lUserId, IDataReader P_oDbDataReader)
        {
           var l_lstFolderTypes = new List<FolderType>();

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        var l_oFolderType = new FolderTypeRepository();

                        l_oFolderType.Identifier = Convert.ToInt32(P_oDbDataReader["RP_TD_ID"].ToString());
                        l_oFolderType.Name = P_oDbDataReader["RP_TD_NOM"].ToString();
                        l_oFolderType.Table = P_oDbDataReader["RP_TD_TABLE"].ToString();
                        l_oFolderType.Label = P_oDbDataReader["RP_TD_LIBELLE"].ToString();
                        l_oFolderType.NumberOfIndex = Convert.ToInt32(P_oDbDataReader["RP_TD_NBINDEX"].ToString());
                        l_oFolderType.Rank = Convert.ToInt32(P_oDbDataReader["RP_TD_POSITION"].ToString());
                        
                        //** FPE: Décommenter lorsque que toutes les procédures prendront en compte le parametre
                        //l_oFolderType.Delete = (bool)FieldRow.Translate(P_oDbDataReader["RP_TD_SUPPR"].ToString());

                        // Add Index to FolderList
                        l_oFolderType.SetIndexList(p_lUserId);
                        // Add Item to FolderList
                        l_oFolderType.SetItemList(p_lUserId);

                        // Add to List
                        l_lstFolderTypes.Add(l_oFolderType);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstFolderTypes;
        }

        /// <summary>
        /// Translates the type of the folder.
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        public static List<FolderType> TranslateFolderType(IDataReader DbDataReader)
        {
            var l_lstFolderTypes = new List<FolderType>();

            try
            {
                using (DbDataReader)
                {
                    while (DbDataReader.Read())
                    {
                        var l_oFolderType = new FolderTypeRepository
                        {
                            Identifier = DbDataReader.GetLongValue("RP_TD_ID"),
                            OrganizationIdentifier = DbDataReader.GetLongValue("RE_ID"),
                            Name = DbDataReader.GetStringValue("RP_TD_NOM"),
                            Label = DbDataReader.GetStringValue("RP_TD_LIBELLE"),
                            Rank = DbDataReader.GetIntValue("RP_TD_POSITION"),
                            Delete = DbDataReader.GetBooleanValue("RP_TD_SUPPR"),
                            Description = DbDataReader.GetStringValue("RP_TD_DESCRIPTION")
                        };

                        // Add Index to FolderList
                        l_oFolderType.SetIndexList();
                        l_oFolderType.SetGroupList();

                        // Add to List
                        l_lstFolderTypes.Add(l_oFolderType);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstFolderTypes;
        }

        /// <summary>
        /// Translates the type of the one folder.
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        public static FolderType TranslateOneFolderType(IDataReader DbDataReader)
        {
            var l_lstFolderType = new FolderType();

            try
            {
                using (DbDataReader)
                {
                    while (DbDataReader.Read())
                    {
                        l_lstFolderType = new FolderType
                        {
                            Identifier = DbDataReader.GetLongValue("RP_TD_ID"),
                            OrganizationIdentifier = DbDataReader.GetLongValue("RE_ID"),
                            Name = DbDataReader.GetStringValue("RP_TD_NOM"),
                            Label = DbDataReader.GetStringValue("RP_TD_LIBELLE"),
                            Rank = DbDataReader.GetIntValue("RP_TD_POSITION"),
                            Delete = DbDataReader.GetBooleanValue("RP_TD_SUPPR"),
                            Description = DbDataReader.GetStringValue("RP_TD_DESCRIPTION")
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstFolderType;
        }
    }
}
